import { routerRedux } from 'dva/router';
import { logInfo,imgupload } from '../services/indexpage';
import {
	notification,Icon
  } from 'antd';
export default {
	namespace: 'indexpage',

	state: {
		collapsed: false,
		username: '',
		vip: false,
		DrawerVisible:false,
		UploadLoading:false,
		imageUrl:null
	},

	subscriptions: {
		setup({ dispatch, history }) {
			// eslint-disable-line
			history.listen((location) => {
				if (location.pathname.includes('/indexpage')) {
					var token = sessionStorage.getItem('token');
					if (token) {
						dispatch({
							type: 'logininfo',
							payload: {
								token: token
							}
						});
					} else {
						dispatch({
							type: 'gologin',
							payload: {}
						});
					}
				} else {
					sessionStorage.clear();
				}
			});
		}
	},

	effects: {
		*fetch({ payload }, { call, put }) {
			// eslint-disable-line
		},
		*gologin({ payload }, { call, put }) {
			// eslint-disable-line
			yield put(
				routerRedux.push({
					pathname: '/login'
				})
			);
		},
		*logininfo({ payload }, { call, put }) {
			// eslint-disable-line
			const result = yield call(logInfo, payload);
			console.log(result);
			if (result.data) {
				var username = result.data.result.name;
				var vip;
				if (result.data.vip.data[0] == 1) {
					vip = true;
				} else vip = false;
				yield put({
					type: 'setState',
					payload: {
						username: username,
						vip: vip,
						avater:result.data.result.img
					}
				});
			}
		},
		*userInfo({ payload }, { call, put }) {
			// eslint-disable-line
			const result = yield call(logInfo, payload);
			console.log(result);
		
			if (result.data) {
				var username = result.data.result.name;
				var vip;
				if (result.data.vip.data[0] == 1) {
					vip = true;
				} else vip = false;
				yield put({
					type: 'setState',
					payload: {
						username: username,
						vip: vip,
						
					}
				});
			}
		},
		*upload({ payload }, { call, put }) {
			// eslint-disable-line
			console.log(123)
			const result = yield call(imgupload, payload);
			console.log(result);
			if (result.data) {
				notification.open({
					message: '头像上传成功',
					description: '您已成功更改用户头像',
					icon: <Icon type="smile" style={{ color: 'green' }} />,
				  });
			}else {
				notification.open({
					message: '头像上传失败',
					description: '更改用户头像失败',
					icon: <Icon type="smile" style={{ color: 'red' }} />,
				  });
			}
		},
	},
	

	reducers: {
		setState(state, action) {
			return {
				...state,
				...action.payload
			};
		}
	}
};
